<?php

namespace app\api\controller;

use app\BaseController;
use Exception;
use service\vo\admin\Admin;
use think\response\Json;

class AdminApi extends BaseController
{
    /**
     * 添加管理员
     */
    public function add_admin() : Json
    {
        try {
            //用户名
            $username = request()->post('username');
            //密码
            $password = request()->post('password');
            if(empty($username)){
                throw new Exception("用户名必传");
            }
            if(empty($password)){
                throw new Exception("密码必传");
            }

            $admin = new Admin();
            $admin->setUsername($username);
            $admin->setPassword($password);
            $id = $this->getAdminService()->addAdmin($admin);
            $data = ['id' => $id];
            $this->requestSuccessReturn($data);
        }catch (Exception $e){
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }
}